import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { PostRo, PostsService } from './posts.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreatePostDto } from './dto/create-post.dot';
import { QueryAllPostsDto } from './dto/query-all-posts.dot';

@ApiTags('文章')
@Controller('posts')
export class PostsController {
  constructor(private readonly postsService: PostsService) {};

  /**
   * 创建文章
   * @param post 
   */
  @ApiOperation({summary: '创建文章'})
  @Post('createPost')
  async create(@Body() post: CreatePostDto) {
    return await this.postsService.create(post);
  }

  /**
   * 获取所有文章
   */
  @ApiOperation({summary: '获取所有文章'})
  @Get('fundAllPosts')
  async findAll(@Query() query: QueryAllPostsDto): Promise<PostRo> {
    return await this.postsService.findAll(query);
  };

  /**
   * 获取指定的文章
   * @param id
   */
  @ApiOperation({summary: '获取指定的文章'})
  @Get('fundPost')
  async fundById(@Query('id') id) {
    return await this.postsService.findById(id);
  }

  /**
   * 更新文章
   * @param id
   * @param post
   */
  @ApiOperation({summary: '根据id更新文章'})
  @Put('updatePos')
  async update(@Param('id') id, @Body() post) { 
    return await this.postsService.update(id, post);
  }

  /**
   * 删除文章
   * @param id
   */
  @ApiOperation({summary: '根据id删除文章'})
  @Delete('deletePost')
  async delete(@Param('id') id) {
    return await this.postsService.remove(id);
  }
}
